/**
 *
 */
package com.common.utils.IDcreate;

import com.common.utils.Encodes;
import com.common.utils.SnowflakeIdWorker;
import com.common.utils.config.Global;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionIdGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 *
 * @author Michael
 * @version 2013-01-15
 */
@Service
@Lazy(false)
public class IdGen implements IdGenerator, SessionIdGenerator {

    private static SecureRandom random = new SecureRandom();
    private static SnowflakeIdWorker idWorker = new SnowflakeIdWorker(
            /*0,
			1);*/
            Integer.parseInt(Global.getConfig("SnowflakeIdWorker_workerId")),
            Integer.parseInt(Global.getConfig("SnowflakeIdWorker_datacenterId")));

    /**
     * SnowflakeIdWorker 产生的ID，平台ID生成器 2017-5-24
     *
     * @return
     */
    public static String nextSFID() {
        return String.valueOf(idWorker.nextId());
    }

    /**
     * 该方法用于生成订单号
     *
     * @param
     */
    public static String orderNumber() {
        return Base22();
    }

    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 5位随机数加上日期yyMMddHHmmssSSS组合的字符串（20位，用于appId）
     */
    public static String Base20() {
        StringBuffer buf = new StringBuffer("a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
        buf.append(",A,B,C,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        buf.append(",1,2,3,4,5,6,7,8,9,0");
        String[] arr = buf.toString().split(",");
        StringBuffer b = new StringBuffer();
        Random r;
        int k;
        for (int i = 0; i < 5; i++) {
            r = new Random();
            k = r.nextInt();
            b.append(String.valueOf(arr[Math.abs(k % 60)]));
        }
        String dateString = new SimpleDateFormat("MMddHHmmssSSS").format(new Date());
        b.append(dateString);
        return b.toString();
    }

    /**
     * 6位随机数验证码
     */
    public static String Base6() {
        int i = (int) ((Math.random() * 9 + 1) * 100000);
        return String.valueOf(i);
    }

    /**
     * 使用SecureRandom随机生成Long.
     */
    public static long randomLong() {
        return Math.abs(random.nextLong());
    }

    /**
     * 基于Base62编码的SecureRandom随机生成bytes.
     */
    public static String randomBase62(int length) {
        byte[] randomBytes = new byte[length];
        random.nextBytes(randomBytes);
        return Encodes.encodeBase62(randomBytes);
    }

    /**
     * Activiti ID 生成
     */
    public String getNextId() {
        return IdGen.uuid();
    }

    public Serializable generateId(Session session) {
        return IdGen.uuid();
    }

    /**
     * 日期yyyyMMddHHmmssSSS +5位随机数 组合的字符串（22位，用于批次号）
     */
    public static String Base22() {
        StringBuffer buf = new StringBuffer("a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
        buf.append(",A,B,C,D,E,F,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z");
        buf.append(",1,2,3,4,5,6,7,8,9,0");
        String[] arr = buf.toString().split(",");
        StringBuffer b = new StringBuffer();
        Random r;
        int k;
        for (int i = 0; i < 5; i++) {
            r = new Random();
            k = r.nextInt();
            b.append(String.valueOf(arr[Math.abs(k % 60)]));
        }
        String dateString = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());

        return dateString + b.toString();
    }

    public static String nextLoginName() {
        StringBuffer buf = new StringBuffer("a,b,c,d,e,f,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z");
        String[] arr = buf.toString().split(",");
        StringBuffer b = new StringBuffer();
        Random r;
        int k;
        for (int i = 0; i < 5; i++) {
            r = new Random();
            k = r.nextInt();
            b.append(String.valueOf(arr[Math.abs(k % 15)]));
        }
        return b.toString() + Base6();
    }

    public static void main(String[] args) {
        System.out.println(randomBase62(8));
		/*System.out.println(IdGen.uuid());
		System.out.println(IdGen.uuid().length());
		System.out.println(new IdGen().getNextId());
		for (int i = 0; i < 1000; i++) {
			System.out.println(IdGen.randomLong() + "  " + IdGen.randomBase62(5));
		}*/
    }

    public static String randomLoginName() {

        return "st_" + randomBase62(10);
    }

    public static String randomPassword() {

        return randomBase62(5);
    }

}
